<template>
	<view v-if="pageShow">
		<view class="headimg flexcenter" @click="uploadImg(1,avatar,avatar_text)">
			<image :src="avatar_text" mode=""></image>
			<view class="xiangji"></view>
		</view>
		<view class="ffffff mainpadding_top flexbetween" style="margin-top: 60rpx;">
			<view class="titletext hei_text bold">昵称</view>
			<input type="text" placeholder="请输入" style="text-align: right;" class="hei_text main_size bold" v-model="nickname">
		</view>
		<view class="gudingdb ffffff mainpadding" style="bottom: 0;">
			<view class="bigbtn" @click="submit">保存</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				avatar:"",
				avatar_text:"",
				nickname:"",
				pageShow:false
			}
		},
		onShow() {
			this.init()
		},
		methods: {
			submit(){
				httpRequest.request('/api/user/profile', 'GET', {
					nickname:this.nickname,
					avatar:this.avatar
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg);
					if(res.code==1){
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			init() {
				httpRequest.request('/api/user/index', 'GET', {}, false, false, true).then(res => {
					this.avatar = res.data.avatar
					this.avatar_text = res.data.avatar_text
					this.nickname = res.data.nickname
					this.pageShow = true
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			uploadImg(value, upImgs,upImgs_text) {
				let _this = this;
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(1, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs,upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(1, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs,upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg,upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.avatar = res.url;
					this.avatar_text = image_item
				}).catch(err => {});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.headimg {
		width: 180rpx;
		height: 180rpx;
		margin: 30rpx auto 0;
		padding-top: 30rpx;
		position: relative;
		image {
			width: 101%;
			height: 100%;
			border-radius: 50%;
		}

		.xiangji {
			width: 40rpx;
			height: 40rpx;
			position: absolute;
			bottom: 0;
			right: 8rpx;
			background-size: 100% 100%;
			background-image: url('@/static/image/sysem/avatarup.png');
		}
	}
</style>